package kr.co.choongang;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;


@WebServlet("/Upload")
@MultipartConfig(maxFileSize=1023*1024*2, location="C:\\file")


public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String getFilename(Part part) {
		String fileName=null;
		String contentDispositionHeader=part.getHeader("content-disposition");
		String [] elements = contentDispositionHeader.split(";");
		for(String element: elements) {
			if(element.trim().startsWith("filename")) {
				fileName=element.substring(element.indexOf('=')+1);
				fileName=fileName.trim().replace("\"","" );
			}
		}
		return fileName;
	}


	

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("EUC-KR");
		
		Part part=request.getPart("theFile");
		String fileName=getFilename(part);
		if(fileName != null&& !fileName.isEmpty()) {
			part.write(fileName);
		}
		String author=request.getParameter("theAuthor");
		author=new String(author.getBytes("iso-8859-1"), "EUC-KR");
		
		response.setContentType("text/html; charset=EUC-KR");
		PrintWriter out=response.getWriter();
		
		out.print("작성자:"+author+"<br>");
		out.print("파일명:"+fileName+"<br>");
		out.print("파일명:<a href='FileDown?file_name="+fileName+"'>"+fileName+"</a><br>");
		out.print("파일크기:"+part.getSize()+"<br>");
	}

}
